package com.jxnu.duewang.zmly.fragment;


import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jxnu.duewang.zmly.R;
import com.jxnu.duewang.zmly.zhangHaoQi.BottomInfo;
import com.jxnu.duewang.zmly.zhangHaoQi.Classify;
import com.jxnu.duewang.zmly.zhangHaoQi.Httpsever;
import com.jxnu.duewang.zmly.zhangHaoQi.Info;
import com.jxnu.duewang.zmly.zhangHaoQi.MyItemDecoration;
import com.jxnu.duewang.zmly.zhangHaoQi.RecyclerviewAdapter;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClassifyFragment extends BaseFragment implements Callback<Info> {



    private RecyclerView recycleview;
    private RecyclerviewAdapter adapter;

    public ClassifyFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmnetTitle() {
        return "分类";
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_classify, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Httpsever httpsever=retrofit.create(Httpsever.class);
        httpsever.getsever("and-a1","android","13","2").enqueue(this);
        recycleview = (RecyclerView) view.findViewById(R.id.classifyrecylerview);
        Httpsever httpsever1=retrofit.create(Httpsever.class);
        httpsever.getbottom("android","cata_index_banner","wifi","0","4.3.98").enqueue(new Callback<BottomInfo>() {
            @Override
            public void onResponse(Call<BottomInfo> call, Response<BottomInfo> response) {
                // List<Bottomimage> bottomimageList=response.body().getList();
                //adapter.setviewpagerlist(bottomimageList);
            }

            @Override
            public void onFailure(Call<BottomInfo> call, Throwable t) {

            }
        });
    }

    @Override
    public void onResponse(Call<Info> call, Response<Info> response) {
        final List<Classify> list=response.body().getList();
        adapter = new RecyclerviewAdapter(list,getContext());
        recycleview.setAdapter(adapter);
        final GridLayoutManager layoutManager=new GridLayoutManager(getContext(),2);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return adapter.isHeader(position)?layoutManager.getSpanCount():1;
            }
        });
        recycleview.setLayoutManager(layoutManager);
        recycleview.addItemDecoration(new MyItemDecoration());
    }

    @Override
    public void onFailure(Call<Info> call, Throwable t) {

    }
}
